package com.axend.aerosense.room.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.ILoginStatusChanged;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.CustomTipsDialog;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomEditFragmentBinding;
import com.axend.aerosense.room.entity.RoomEditListBean;
import com.axend.aerosense.room.entity.RoomListBean;
import com.axend.aerosense.room.entity.RoomSortInfoBean;
import com.axend.aerosense.room.entity.RoomSortListBean;
import com.axend.aerosense.room.listener.ItemTouchHelperCallback;
import com.axend.aerosense.room.ui.activity.RoomAddActivity;
import com.axend.aerosense.room.ui.activity.RoomEditActivity;
import com.axend.aerosense.room.ui.adapter.RoomEditTouchAdapter;
import com.axend.aerosense.room.viewmodel.RoomEditViewMode;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class RoomEditFragment
        extends MvvmBaseFragment<RoomEditFragmentBinding, RoomEditViewMode>
        implements ILoginStatusChanged, ILoadDataResult<RoomSortListBean> {

    private RoomEditTouchAdapter adapter;

    public static RoomEditFragment getInstance(){
        RoomEditFragment fragment = new RoomEditFragment();
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_edit_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected RoomEditViewMode getViewModel() {
        BaseViewModelFactroy<RoomSortListBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        return new ViewModelProvider(this, myViewModelFactroy).get(RoomEditViewMode.class);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setLoadSir(viewDataBinding.roomConstraintLayout);
        init();
    }

    private void init() {
        adapter = new RoomEditTouchAdapter();
        ItemTouchHelperCallback itemTouchHelperCallback = new ItemTouchHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
        viewDataBinding.roomEditList.setLayoutManager(new LinearLayoutManager(getContext()));
        viewDataBinding.roomEditList.setAdapter(adapter);
        itemTouchHelper.attachToRecyclerView(viewDataBinding.roomEditList);

//        List<RoomEditListBean.RoomInfo> list = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            list.add(new RoomEditListBean.RoomInfo(i + "", i));
//        }
//        adapter.upDateList(list);

        adapter.setOnClickListener((position, roomInfo) -> {
            //删除房间
            viewModel.deleteRoom(getActivity(),roomInfo.getRoomUuid() ,new IOperationDetailResult<String>() {
                @Override
                public void onSuccess(String s) {
                    ToastUtils.showShort(R.string.common_success);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM ,Integer.class).postValue(1);
                    adapter.getList().remove(roomInfo);
                    adapter.notifyItemRemoved(position);
                }

                @Override
                public void onFail(int errCode, String message) {
                    if(Gobal.ERROR_CODE.ERR_LOGIN == errCode){
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();

                    }else if(errCode == Gobal.ERROR_CODE.ERR_DELETE){
                        CustomTipsDialog dialog = new CustomTipsDialog(getActivity(), getString(R.string.room_delete_tips));
                        dialog.setOnClickBottomListener(new CustomTipsDialog.OnClickBottomListener(){

                            @Override
                            public void onPositiveClick() {
                                dialog.dismiss();
                            }

                            @Override
                            public void onNegtiveClick() {
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                    }else{
                        ToastUtils.showShort(message);
                    }
                }

            });
        });

        ((RoomEditActivity) getActivity()).setRightText(R.string.picker_sure, v -> {
            //保存排序好的房间
            List<RoomSortInfoBean> list = adapter.getList();
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRoomSort(i);
            }
            viewModel.setAllRoomSort(getActivity(), new RoomListBean(list));
        });

        viewModel.onLoad();
        showLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        ((RoomEditActivity) getActivity()).setTitle(getString(R.string.room_edit_rooms));
    }

    @Override
    protected void onRetryBtnClick() {
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(RoomSortListBean bean) {
        if (bean != null && bean.getRoomSortInfo().size() > 0){
            adapter.upDateList(bean.getRoomSortInfo());
        }
    }

    @Override
    public void loadDataFail(String decs) {
        showFailure(decs);
    }

    @Override
    public void loginStatusChanged() {

    }
}
